﻿using System;
using DBProvider;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCNewEdit : System.Web.UI.UserControl
	{
		private INewsProvider _NewsProvider;

		public string NewsTitle
		{
			get
			{
				return CommonHelper.QueryString("newstitle");
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindNews();
			}
		}

		protected void btnSave_Click(object sender, EventArgs e)
		{
			if (!string.IsNullOrEmpty(NewsTitle))
			{
				if (_NewsProvider == null)
				{
					_NewsProvider = new NewsProvider();
				}

				var theNews = _NewsProvider.GetSiteNewByTitle(NewsTitle);

				if (theNews != null)
				{
					theNews.NewsContent = txtNewsContent.Text.Trim();

					if (_NewsProvider.UpdateSiteNew(theNews))
					{
						ltrMsg.Text = "保存成功";
					}
					else
					{
						ltrMsg.Text = "保存失败";
					}
				}
				else
				{
					theNews = new SiteNew() { NewsContent = txtNewsContent.Text.Trim(), NewsAddTime = DateTime.Now, NewsTitle = "社区信息" };

					var msg = string.Empty;
					if (_NewsProvider.CreateSiteNew(theNews, out msg) != null)
					{
						ltrMsg.Text = "保存成功";
					}
					else
					{
						ltrMsg.Text = "保存失败";
					}
				}
			}
		}

		private void BindNews()
		{
			if (!string.IsNullOrEmpty(NewsTitle))
			{
				if (_NewsProvider == null)
				{
					_NewsProvider = new NewsProvider();
				}

				var theNews = _NewsProvider.GetSiteNewByTitle(NewsTitle);

				if (theNews != null)
				{
					txtNewsContent.Text = theNews.NewsContent;
				}
			}
		}
	}
}